package com.bwie.service.impl;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.utils.Result;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
* @author 28242
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-05-21 15:59:15
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{
    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    FastFileStorageClient fastFileStorageClient;

    @Override
    public Result updateUser(TbUser tbUser) {

        TbUser user = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserId, tbUser.getUserId())
        );
        if (user==null){
            return Result.error().setMsg("用户不存在");
        }

        tbUserMapper.updateById(tbUser);
        return Result.ok();
    }

    @Override
    public Result uploade(MultipartFile file) {

        String extName = FileUtil.extName(file.getOriginalFilename());
        try {
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return Result.ok().setData(url);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error().setMsg("上传失败");

        }
    }

    @Override
    public Result deleteUser(Integer userId) {

        //判断用户是否存在
        TbUser tbUser = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserId, userId)
        );
        if (tbUser==null){
            return Result.error().setMsg("用户不存在");
        }

        //删除用户
        tbUserMapper.deleteById(userId);
        return Result.ok();
    }

    @Override
    public Result DeleteAllByUser(List<TbUser> tbUsers) {

        if (tbUsers==null || tbUsers.size()==0){
            return Result.error().setMsg("请选择商户");
        }


       tbUsers.stream().forEach(item->{
           tbUserMapper.deleteById(item);
       });
        return Result.ok();
    }

    @Override
    public Result checkUser(TbUser tbUser) {

        //审核不通过必须填写原因
        if (tbUser.getUserStatus()==2){
            if (StringUtils.isEmpty(tbUser.getReason())){
                return Result.error().setMsg("请输入审核原因");
            }
        }
        //判断用户是否存在
        TbUser user = tbUserMapper.selectById(tbUser.getUserId());
        if (user==null){
            return Result.error().setMsg("用户不存在");
        }

        //进行审核
        user.setUserStatus(tbUser.getUserStatus());
        user.setReason(tbUser.getReason());
        tbUserMapper.updateById(user);


        return Result.ok();
    }


}




